
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <link rel="stylesheet" type="text/css" href="style3.css"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>




    <? // displaying the text fields where the users are required to enter the email and password and confirm the password as well as a button to submit the data ?>
</head>
<body>

    <form method="post" Action ="" target="_self">
        <fieldset>
        <legend style="font-size: 12pt"><b>Sign Up</b></legend>
        <table>
            <tr>
                <td>
                    Email:
                </td>
                <td>
                    <input type ="text" name ="Email" maxlength ="50"/>
                </td>
            </tr>
            <tr>
                <td>
                    Password:
                </td>
                <td>
                    <input type ="password" name ="Password" maxlength ="30"/>
                </td>
            </tr>
            <td>
                Confirm Password:
            </td>
            <td>
                <input type ="password" name ="Cpassword" maxlength ="30"/>
            </td>
            </tr>
            <tr>
                <td></td>
            <tr>
                <td></td>
                <td>
                    <input type ="submit" name ="submit" value ="Register"   />

                </td>

                <td></td>
            </tr>
        </fieldset>


            </tr>
        </table>
    </form>
</body>
</html>
<?php
ob_start(); //i activated the output buffering


if ($_SERVER['REQUEST_METHOD'] == 'POST') { // this conditions checks whether the user clicks on the button ( a post request )
    // if conditions to check on each text field to check if they are empty
    if ($_POST['Email'] == "") {
        echo '<script type="text/javascript">',
        'alert("please enter a valid email");',
        '</script>';
    } else {
        if ($_POST['Password'] == "") {
            echo '<script type="text/javascript">',
            'alert("please enter a valid password");',
            '</script>';
        } else {
            if ($_POST['Cpassword'] == "") {
                echo '<script type="text/javascript">',
                'alert("please confirm your password");',
                '</script>';
            } else {
                if ($_POST['Password'] == $_POST['Cpassword']) { // this conditions that checks the whether the password matchs
                    SignUp($_POST['Email'], $_POST['Password']); // calls the sign up funtion
                } else {
                    echo '<script type="text/javascript">',
                    'alert("the password  does not match");',
                    '</script>';
                }
            }
        }
    }//end of the else part where i check for text fields and whether the password matchs or not
}// end of the first if condition

Function SignUp($email, $password) {
    $email = filter_var($_POST['Email'], FILTER_SANITIZE_EMAIL);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
    } else {
        //establishing a database connection
        $con = mysql_connect("localhost", "root", "");
        if (!$con) {
            die('Could not connect:' . mysql_error()); // displaying an erorr message if the connection can not be established
        }
        mysql_select_db("mydb", $con);

//checks if the email already exists
        $check_if_exists = mysql_query("Select m.member_id From member m, emails e Where (m.member_id = e.member_id AND e.email = '$email')OR(m.email = '$email')") or die(mysql_error());
        if (mysql_num_rows($check_if_exists) == 0) {
            //insert the email and password in the database
            $result = mysql_query("INSERT INTO Member(email, password) VALUES ('$email','$password')")
                    or die(mysql_error()); // displaying an error message if the query failed

            $_SESSION ['email'] = $_POST['Email']; // store the email in a session
			confirmsignup($_POST['Email']);

        } else {
            echo "this email already exists";
        }
        ob_end_flush(); //ending the output buffering so that the header can be fired now
    }
}
       function confirmsignup($email){
 //opening connection
    $conn = mysql_connect('localhost', 'root', '') or die(mysql_error());
    mysql_select_db('mydb') or die(mysql_error());
    
    // a query to get memberid 
    $myquery = mysql_query("
         SELECT `member_id`
         FROM `member`m
         WHERE m.`email`='$email'
         ") or die(mysql_error());
    
  
    // fetching the memberid
    $myrow = mysql_fetch_assoc($myquery);
    // saving the member id in a  variable
    $memberid = $myrow["member_id"];

     $subject = 'Confirmation mail';  //email subject

                $message = <<<EMAIL
Please comfirm your account through this link  http://localhost:88/ConfO/index.php?member_id=$memberid&new=TRUE;
EMAIL;
   // the email message which redirects to the member's homepage

                $from = 'marmarwalid@hotmail.com';
                $headers = "From:" . $from;
                mail($email, $subject, $message, $headers);  //the email header

              echo '<script type="text/javascript">',
        'alert("Please confirm your account through your email");',
        '</script>'; // alerting the user that he has to confirm his account

}

?>
